<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

        <el-button
          type="danger"
          plain
          icon="el-icon-plus"
          size="mini"
          v-if="viewType=='selectpro'"
          @click="confirmSelect"
        >确定选择</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="libraryList" @selection-change="handleSelectionChange" @row-click="singleElection">
      <el-table-column label="序号" width="65" v-if="viewType=='selectpro'">
        <template slot-scope="scope">
          <el-radio
            class="radio"
            v-model="templateSelection"
            :label="scope.$index + 1"
            :value="scope.row.id"
            tabindex="0"
          >
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column type="selection"   v-if="viewType!='selectpro'" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="原始文件上传S3地址" align="center" prop="s3Url" >
        <template slot-scope="scope">
          <el-link :href="`${previewUrl}${scope.row.s3Url}`" :underline="false" target="_blank">
            <span class="el-icon-document"> {{scope.row.s3Url }} </span>
          </el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listLibrary, getLibrary, delLibrary, addLibrary, updateLibrary } from "@/api/business/library";

export default {
  name: "Library",
  props: {
    viewType: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      previewUrl: process.env.VUE_APP_API_BASE_URL,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专题表格数据
      libraryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dataId: null,
        title: null,
        content: null,
        keyword: null,
        abstract: null,
        publisher: null,
        source: null,
        topicType: null,
        category: null,
        nationality: null,
        verifyStatus: null,
        publicStatus: null,
        publishTime: null,
        insertTime: null,
        s3Url: null,
        s3UrlWord: null,
        tags: null,
        entityInfo: null,
        relaInfo: null,
        hignFreqW: null,
        fixedColls: null,
        fixedCollsSegments: null,
        hignFreqSs: null,
        techDescs: null,
        siteUrl: null,
        siteName: null,
        clusterId: null,
        fileName: null,
        runningStatus: null,
        finishTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dataId: [
          { required: true, message: "数据id不能为空", trigger: "blur" }
        ],
      },
      templateSelection:null,
      selectRow:null,
    };
  },
  created() {
    this.getList();
  },
  methods: {

    singleElection (row) {
      console.log(row)
      this.selectRow=row
      this.templateSelection = this.libraryList.indexOf(row)+1;
      this.ids= row.id;
    },

    confirmSelect() {
      if(!this.selectRow){
        this.$modal.msgError("请选择一条记录");
        return
      }
      // 传递个父组件
      this.$emit('selectJlProduct',this.selectRow)

    },


    /** 查询专题列表 */
    getList() {
      this.loading = true;
      listLibrary(this.queryParams).then(response => {
        this.libraryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dataId: null,
        title: null,
        content: null,
        keyword: null,
        abstract: null,
        publisher: null,
        source: null,
        topicType: null,
        category: null,
        nationality: null,
        verifyStatus: null,
        publicStatus: null,
        publishTime: null,
        updateTime: null,
        insertTime: null,
        s3Url: null,
        s3UrlWord: null,
        tags: null,
        entityInfo: null,
        relaInfo: null,
        hignFreqW: null,
        fixedColls: null,
        fixedCollsSegments: null,
        hignFreqSs: null,
        techDescs: null,
        siteUrl: null,
        siteName: null,
        clusterId: null,
        fileName: null,
        runningStatus: null,
        finishTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLibrary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改专题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLibrary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLibrary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除专题编号为"' + ids + '"的数据项？').then(function() {
        return delLibrary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/library/export', {
        ...this.queryParams
      }, `library_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
